<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <script src="./libs/jquery.twbsPagination.js"></script>
    <!-- 引入模板引擎js文件 -->
    <script src="./libs/template-web.js"></script>

</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章列表
        </div>
        <div class="container-fluid common_con">
            <div class="row opt_btns">
                <div class="col-xs-6">
                    <form class="form-inline">
                        <select id="selCategory" name="" class="form-control input-sm">
                            <option value="">所有分类</option>
                            <option>未分类</option>
                            <option>奇趣事</option>
                            <option>会生活</option>
                            <option>爱旅行</option>
                        </select>
                        <select id="selStatus" name="" class="form-control input-sm">
                            <option value="">所有状态</option>
                            <option value="草稿">草稿</option>
                            <option value="已发布">已发布</option>
                        </select>
                        <button id="btnSearch" class="btn btn-default btn-sm">筛选</button>
                    </form>
                </div>
                <div class="col-xs-6">
                    <a href="article_release.html" class="btn btn-success btn-sm pull-right" id="release_btn">发表文章</a>
                </div>
            </div>
            <table class="table table-striped table-bordered table-hover mp20">
                <thead>
                    <tr>
                        <th>标题</th>
                        <th>作者</th>
                        <th>分类</th>
                        <th class="text-center">发表时间</th>
                        <th class="text-center">状态</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>王积千造统最头</td>
                        <td>杰克</td>
                        <td>奇趣事</td>
                        <td class="text-center">2017-06-08 07:08:46</td>
                        <td class="text-center">已发布</td>
                        <td class="text-center">
                            <a href="article_edit.html" class="btn btn-default btn-xs">编辑</a>
                            <a href="javascript:void(0);" class="btn btn-danger btn-xs delete">删除</a>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div class="row text-center ">
                <ul id="pagination-demo" class="pagination-sm nav-tip"></ul>
            </div>
        </div>
    </div>
</body>
<script src="./libs/https.js"></script>
<script type="text/html" id="tpl-list">
    <option value="">所有分类</option>
    {{each}}
    <option value="{{$value.id}}">{{$value.name}}</option>
    {{/each}}
</script>
<script type="text/html" id="tpl-query">
    {{each}}
    <tr>
        <td>{{$value.title}}</td>
        <td>{{$value.author}}</td>
        <td>{{$value.category}}</td>
        <td class="text-center">{{$value.date}}</td>
        <td class="text-center">{{$value.state}}</td>
        <td class="text-center">
            <a data-id="{{$value.id}}" href="article_edit.html" class="btn btn-default btn-xs btn-edit">编辑</a>
            <a data-id="{{$value.id}}" href="javascript:void(0);" class="btn btn-danger btn-xs delete">删除</a>
        </td>
    </tr>
    {{/each}}
</script>

<script>
    $(function () {
        let delPage = 1
        //
        $.ajax({
            type: 'get',
            url: BigNew.category_list,
            success: function (res) {


                let arr = res.data
                let htmlStr = template('tpl-list', arr)
                $('#selCategory').html(htmlStr)
            }

        })
        //封装加载页面
        function loaddata(page) {
            $.ajax({
                type: 'get',
                url: BigNew.article_query,
                data: {
                    type: $('#selCategory').val(),
                    state: $('#selStatus').val(),
                    //文章加载在第几页，一般刚开始是在第一页，后面动态加载
                    page: page,
                    //每页显示新闻数
                    perpage: 5,
                },
                success: function (res) {

                    let arr = res.data.data
                    let htmlStr = template('tpl-query', arr)
                    $('tbody').html(htmlStr)
                    if (res.data.totalCount > 0) {
                        //把之前的函数删掉，totalPages只能加载一次 ，太垃圾了
                        $('#pagination-demo').twbsPagination('destroy');
                        //调用分页导航
                        $('#pagination-demo').twbsPagination({
                            totalPages: res.data.totalPage,
                            visiblePages: 10,
                            //一开始UI选中的页数
                            startPage: page,
                            initiateStartPageClick: false,
                            onPageClick: function (event, page) {
                                loaddata(page)
                                //获取用户在那一页
                                delPage = page
                            },

                            //改中文
                            first: '首页',
                            last: '尾页',
                            prev: '上一页',
                            next: '下一页',
                        });
                    } else {
                        $('.nav-tip').html('这个分类的傻逼太少')
                    }

                }
            })
        }
        loaddata(1)

        //搜索
        $('#btnSearch').on('click', function (e) {
            e.preventDefault()
            loaddata(1)
        })

        //删除功能
        $('tbody').on('click', '.delete', function () {
            let id = $(this).attr('data-id');

            $.ajax({
                type: 'post',
                url: BigNew.article_delete,
                data: {
                    id: id
                },
                success: function (res) {

                    //删完重新加载,不要重新跳转页数

                    //处理首页和尾页
                    if ($('tbody tr').length > 1 || delPage == 1) {
                        loaddata(delPage)
                    } else {
                        loaddata(--delPage)
                    }
                }
            })
        })

        //编辑跳转
        $('tbody').on('click', '.btn-edit', function (e) {
            e.preventDefault()
            window.location = './article_edit.html'
            let id = $(this).attr('data-id')
            localStorage.setItem('id', id)
        })

    })
</script>

</html>